﻿using System;

namespace SimpleTFTP
{
    internal abstract class BasePacket
    {
        public OpCode OpCode { get; set; }

        public static BasePacket CreatePacket(byte[] data)
        {
            // packet must be at least 4 bytes long
            if (data.Length < 4)
            {
                throw new NotImplementedException();
            }
            else
            {
                // get the op code
                OpCode opcode = (OpCode)data[1];

                // return required packet here
                switch (opcode)
                {
                    case OpCode.RRQ:
                        return new ReadWritePacket(data);

                    case OpCode.WRQ:
                        return new ReadWritePacket(data);

                    case OpCode.ACK:
                        return new AckPacket(data);

                    case OpCode.DATA:
                        return new DataPacket(data);

                    default:
                        //throw new NotImplementedException();
                        Console.WriteLine("Not implemented: {0}", opcode);
                        return null;
                }
            }
        }

        public abstract byte[] ToByteArray();
    }
}